home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / specfun / betai.m < prev    next >
Text File  |  1996-11-19  |  3KB  |  94 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage: betai (a, b, x)
  18. ##
  19. ## Returns the incomplete beta function
  20. ##
  21. ##   betai (a, b, x) = BETA(a,b)^(-1) INT_0^x t^(a-1) (1-t)^(b-1) dt.
  22.  
  23. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  24. ## Created: 2 August 1994
  25. ## Adapted-By: jwe
  26.  
  27. ## Computation is based on the series expansion
  28. ##   betai(a, b, x) 
  29. ##     = \frac{x^a}{B(a, b)}
  30. ##         \sum_{l=0}^\infty \frac{(1-b)\cdots(l-b)}{a+l} \frac{x^l}{l!}
  31. ## for x <= 1/2.  For x > 1/2, betai(a, b, x) = 1 - betai(b, a, 1-x).
  32.  
  33. function y = betai (a, b, x)
  34.   
  35.   if (nargin != 3)
  36.     usage ("betai (a, b, x)");
  37.   endif
  38.  
  39.   [retval, x, a, b] = common_size (x, a, b);
  40.   if (retval > 0)
  41.     error ("betai:  a, b and x must be of common size or scalar");
  42.   endif
  43.   
  44.   [r, c] = size (x);
  45.   s = r * c;
  46.   x = reshape (x, 1, s);
  47.   a = reshape (a, 1, s);
  48.   b = reshape (b, 1, s);
  49.   y = zeros (1, s);
  50.  
  51.   k = find ((x < 0) | (x > 1) | !(a > 0) | !(b > 0) | isnan (x));
  52.   if any (k)
  53.     y(k) = NaN * ones (1, length (k));
  54.   endif
  55.  
  56.   k = find ((x == 1) & (a > 0) & (b > 0));
  57.   if any (k)
  58.     y(k) = ones (1, length (k));
  59.   endif
  60.  
  61.   ## Now do the series computations.  
  62.   ## The error when truncating at term L is always less than 2^(-L),
  63.   ## hence the following choice of L. 
  64.  
  65.   L = ceil (-log (eps) / log (2));
  66.   h = ones (L, 1);
  67.   
  68.   k = find ((x >= 0) & (x <= 1/2) & (a > 0) & (b > 0));
  69.   if any (k)
  70.     l   = (1 : L)' * ones (1, length (k));
  71.     tmp = cumprod ((1 - (h * b(k)) ./ l) .* (h * x(k))) ...
  72.     ./ ((h * a(k)) + l);
  73.     y(k) = exp (a(k) .* log (x(k))) .* (1 ./ a(k) + sum (tmp)) ...
  74.     ./ beta (a(k), b(k));
  75.   endif
  76.   
  77.   k = find ((x > 1/2) & (x < 1) & (a > 0) & (b > 0));
  78.   if any (k)
  79.     l   = (1 : L)' * ones (1, length (k));
  80.     tmp = cumprod ((1 - (h * a(k)) ./ l) .* (h * (1 - x(k)))) ...
  81.     ./ (h * b(k) + l);
  82.     y(k) = 1 - exp (b(k) .* log (1 - x(k))) ...
  83.     .* (1 ./ b(k) + sum (tmp)) ./ beta (a(k), b(k));
  84.   endif
  85.  
  86.   y = reshape (y, r, c);
  87.   
  88. endfunction
  89.  
  90.  
  91.  
  92.  
  93.  
  94.